package com.ziq.base.extension

import android.util.Log
import android.view.View
import com.ziq.base.common.Constant

/***
 * 防止快速点击
 */
fun View.click(listener: (view: View) -> Unit) {
    val minTime = 500L
    var lastTime = 0L
    this.setOnClickListener {
        val tmpTime = System.currentTimeMillis()
        if (tmpTime - lastTime > minTime) {
            lastTime = tmpTime
            listener.invoke(this)
        } else {
            Log.d(Constant.LOG_TAG,"点击过快，取消触发")
        }
    }
}

fun View.clickCount(countLimit: Int = 5, listener: (view: View) -> Unit) {
    var count = 0
    val minTime = 1000L
    var lastTime = 0L
    this.setOnClickListener {
        val tmpTime = System.currentTimeMillis()
        if (tmpTime - lastTime > minTime) {
            //超时重置点击次数
            count = 0
            lastTime = 0L
        }

        count++
        lastTime = tmpTime
        if (count >= countLimit) {
            count = 0
            lastTime = 0L
            listener.invoke(this)
        }
    }
}
